package com.forever7776.life.admin.controller.life.user;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.forever7776.life.admin.controller.BaseController;
import com.forever7776.life.core.common.constants.GlobalConstants;
import com.forever7776.life.core.common.enums.SysUserEnum;
import com.forever7776.life.core.common.vo.SysUserSearchVO;
import com.forever7776.life.core.entity.sys.SysUser;
import com.forever7776.life.core.service.sys.ISysUserService;
import enums.ResultEnum;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import util.ResultUtil;
import util.ValidateUtil;

/**
 * <p>
 * 后台系统管理用户
 * </p>
 *
 * @author kz
 * @date 2018-03-06
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;

    @RequestMapping
    public String type(@RequestParam("type") String type, ModelMap map) {
        map.put("type", type);
        return LIFE_ADMIN_VIEW_PAGE_PATH + "user/index";
    }

    /**
     * 系统用户列表
     *
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public JSONObject list(SysUserSearchVO searchVO) {
        JSONObject result = new JSONObject();
        Page<SysUser> page = sysUserService.selectUsersByCondition(new Page<SysUser>(pageNumber, pageSize), searchVO);
        result.put("userStatusMap", SysUserEnum.STATUS_MAP);
        result.put("page", page);
        return result;
    }


    /**
     * 查询所有用户数
     *
     * @return
     */
    @RequestMapping(value = "/stat", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject stat() {
        JSONObject result = new JSONObject();
        result.put("count", sysUserService.selectCount());
        return result;
    }

    /**
     * 根据时间类型获取注册用户数量
     *
     * @return
     */
    @RequestMapping("/register/count/{type}")
    @ResponseBody
    public JSONObject todayRegisterCount(@PathVariable String type) {
        JSONObject result = new JSONObject();
        Integer count = sysUserService.selectRegisterCountByTimeType(type);
        result.put("count", count);
        return result;
    }


    /**
     * 根据时间类型获取登录用户数量
     *
     * @return
     */
    @RequestMapping("/login/count/{type}")
    @ResponseBody
    public JSONObject todayLoginCount(@PathVariable String type) {
        JSONObject result = new JSONObject();
        Integer count = sysUserService.selectLoginCountByTimeType(type);
        result.put("count", count);
        return result;
    }

    /**
     * 批量修改用户状态
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/batch/status/{ids}", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject batchStatus(@PathVariable String ids, @RequestParam(value = "status", defaultValue = "0") Integer status) {
        sysUserService.batchSysUserStatus(ids, status);
        return ResultUtil.getSuccessResult("操作成功！");
    }

    /**
     * 单个修改用户状态
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/update/status/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject updateStatus(@PathVariable String id, @RequestParam(value = "status", defaultValue = "0") Integer status) {
        sysUserService.updateSysUserStatus(id, status);
        return ResultUtil.getSuccessResult("操作成功！");
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject addRole(SysUser user) {
        JSONObject result = checkForm(user, "添加成功！");
        if (result.getInteger(ResultUtil.CODE).equals(ResultEnum.STATUS.FAIL.getCode())) {
            return result;
        }
        user.setHeadimg(GlobalConstants.USER_HEAD_DEFAULT);
        user.setSalt(DigestUtils.md5Hex(user.getPassword()));
        sysUserService.insert(user);
        return result;
    }

    /**
     * 校验用户表单
     *
     * @param user
     * @param msg
     * @return
     */
    public JSONObject checkForm(SysUser user, String msg) {
        if (user == null) {
            return ResultUtil.getFailResult("添加失败~~~");
        }
        if (StringUtils.isEmpty(user.getUsername())) {
            return ResultUtil.getFailResult("用户登录名不能为空~~~");
        }
        if (StringUtils.isEmpty(user.getEmail())) {
            return ResultUtil.getFailResult("用户邮箱不能为空~~~");
        }
        if (!ValidateUtil.verifyEmailIsPassed(user.getEmail())) {
            return ResultUtil.getFailResult("请填写正确的邮箱~~~");
        }
        if (StringUtils.isEmpty(user.getPassword())) {
            return ResultUtil.getFailResult("用户密码不能为空~~~");
        }
        return ResultUtil.getSuccessResult(msg);
    }
}
